package cn.kgc.gmall.controller;

import cn.kgc.gmall.pojo.KeywordStats;
import cn.kgc.gmall.pojo.VisitorStats;
import cn.kgc.gmall.service.VisitorStatsService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/sugar")
public class SugarController {
    // $API_HOST/api/sugar/visitor?date=20220916
    // $API_HOST/api/sugar/hr?date=20220916
    @Autowired
    private VisitorStatsService visitorStatsService;

    @RequestMapping("/keyword")
    public String getKeyword(@RequestParam(name = "date",defaultValue = "0") Integer date,
                             @RequestParam(name = "limit",defaultValue = "20") Integer limit){
        if (date == 0){
            date = now();
        }
        List<KeywordStats> keywordStatsList =  visitorStatsService.getKeyword(date,limit);
        StringBuilder jsonSb=new StringBuilder( "{\"status\":0,\"msg\":\"\",\"data\":[" );
        //循环拼接字符串
        for (int i = 0; i < keywordStatsList.size(); i++) {
            KeywordStats keywordStats =  keywordStatsList.get(i);
            if(i>=1){
                jsonSb.append(",");
            }
            jsonSb.append(  "{\"name\":\"" + keywordStats.getKeyword() + "\"," +
                    "\"value\":"+keywordStats.getCt()+"}");
        }
        jsonSb.append(  "]}");
        return jsonSb.toString();

    }

    @RequestMapping("/visitor")
    public String getVisitor(@RequestParam(name = "date",defaultValue = "0") Integer date ){
        if (date == 0){
            date = now();
        }
        List<VisitorStats> visitorStatsList = visitorStatsService.getVisitorStatsIsNew(date);
        VisitorStats newVisitorStats  = new VisitorStats();
        VisitorStats oldVisitorStats  = new VisitorStats();

        // 根据isNew进行分组  列表中只有两条数据
        for (VisitorStats visitorStats : visitorStatsList) {
            if (visitorStats.getIs_new().equals("1")){
                newVisitorStats = visitorStats;
            }else{
                oldVisitorStats = visitorStats;
            }
        }
        // 拼接sql
        //把数据拼接入字符串
        String json = "{\"status\":0,\"data\":{\"combineNum\":1,\"columns\":" +
                "[{\"name\":\"类别\",\"id\":\"type\"}," +
                "{\"name\":\"新用户\",\"id\":\"new\"}," +
                "{\"name\":\"老用户\",\"id\":\"old\"}]," +
                "\"rows\":" +
                "[{\"type\":\"用户数(人)\"," +
                "\"new\": " + newVisitorStats.getUv_ct() + "," +
                "\"old\":" + oldVisitorStats.getUv_ct() + "}," +
                "{\"type\":\"总访问页面(次)\"," +
                "\"new\":" + newVisitorStats.getPv_ct() + "," +
                "\"old\":" + oldVisitorStats.getPv_ct() + "}," +
                "{\"type\":\"跳出率(%)\"," +
                "\"new\":" + newVisitorStats.getUjRate() + "," +
                "\"old\":" + oldVisitorStats.getUjRate() + "}," +
                "{\"type\":\"平均在线时长(秒)\"," +
                "\"new\":" + newVisitorStats.getDurPerSv() + "," +
                "\"old\":" + oldVisitorStats.getDurPerSv() + "}," +
                "{\"type\":\"平均访问页面数(人次)\"," +
                "\"new\":" + newVisitorStats.getPvPerSv() + "," +
                "\"old\":" + oldVisitorStats.getPvPerSv()
                + "}]}}";



        return json;
    }

    /*
                {
          "status": 0,
          "msg": "",
          "data": {
            "categories": [
              "01",
              "02",
              "03",
              "04",
              "05"
            ],
            "series": [
              {
                "name": "pv",
                "data": [
                  26591,
                  28631,
                  12244,
                  8222,
                  15502

                ]
              }
            ]
          }
        }

     */
    @RequestMapping("/hr")
    public String getHr(@RequestParam(name = "date",defaultValue = "0") Integer date ){
        if (date == 0){
            date = now();
        }

       List<VisitorStats> visitorStatsList =  visitorStatsService.getMidStatsGroupbyHourNewFlag(date);
       // 声明一个24对象数组 每个小时一个对象
       VisitorStats[] visitorStatsHour = new VisitorStats[24];
        // 根据小时数进行赋值
        for (VisitorStats visitorStats : visitorStatsList) {
            visitorStatsHour[visitorStats.getHr()] = visitorStats;
        }

        // 从数组中依次取出数据
        // 时间
        List<String> hour = new ArrayList<>();
        // 1. pv
        List<Long> pv = new ArrayList<>();
        // 2. uv
        List<Long> uv = new ArrayList<>();
        // 3. 新用户uv
        List<Long> new_uv = new ArrayList<>();
        // 依次将数据拿出来
        for (int i = 0; i <= 23; i++) {
            VisitorStats visitorStats = visitorStatsHour[i];
            if (visitorStats != null){
                pv.add(visitorStats.getPv_ct());
                uv.add(visitorStats.getUv_ct());
                new_uv.add(visitorStats.getNew_uv());

            }else{
                pv.add(0L);
                uv.add(0L);
                new_uv.add(0L);
            }
            //小时数不足两位补零
             hour.add(String.format("%02d", i));

        }

        // 拼接json

        String json = "{\"status\":0,\"data\":{" + "\"categories\":" +
                "[\""+StringUtils.join(hour,"\",\"")+ "\"],\"series\":[" +
                "{\"name\":\"uv\",\"data\":["+ StringUtils.join(uv,",") +"]}," +
                "{\"name\":\"pv\",\"data\":["+ StringUtils.join(pv,",") +"]}," +
                "{\"name\":\"新用户\",\"data\":["+ StringUtils.join(new_uv,",") +"]}]}}";


        return json;
    }


    public Integer now(){
        String date = DateFormatUtils.format(new Date(), "yyyyMMdd");
        return Integer.parseInt(date);
    }
}
